/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tools;

import com.openbrew.openbrewrf.FinishedGoodsInventory;
import com.openbrew.openbrewrf.PackageType;
import com.openbrew.openbrewrf.Sales;
import com.openbrew.openbrewrf.ScanTblKeg;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

/**
 *
 * @author dchapman
 */
public class KegTools {
    /***************************************************************************
     * Sale Insertion Actions.
     **************************************************************************/
    /***************************************************************************
     * Do a supply removal without adjusting allocation
     * @param en - Entity Manager
     * @param sale - sale being inserted.
     **************************************************************************/
    public void doSetKegFromSale(final EntityManager en, final Sales sale){
        if(sale.getKegID()!=0){
            TypedQuery<ScanTblKeg> query = en.createQuery(
                    "SELECT s FROM ScanTblKeg s WHERE s.kegID = ?1", ScanTblKeg.class);
            query.setParameter(1, sale.getKegID());
            ScanTblKeg keg = query.getSingleResult();
            keg.setKegCust(sale.getCustID());
            keg.setDateSold(sale.getSDate());
            keg.setScanTime(sale.getSDate());
            en.merge(keg);
            
            //Remove Keg to POSSale
            POSVolumeModifier vm = new POSVolumeModifier();
            vm.doVolumeAddition(en, keg);
        }
    }
    
    /***************************************************************************
     * Keg Packaging Functions
     **************************************************************************/
    /***************************************************************************
     * This function modifies tank volume, beer inventory, and adds movements
     * based on the list of kegs scanned and submitted to openBrewRF.
     * @param en - Entity Manager
     * @param kegs - kegs being scanned.
     **************************************************************************/
    public void doProcessKegPackagingList(final EntityManager en, final List<ScanTblKeg> kegs){
        if(kegs != null){
            final double volumeMoved = calcVol(kegs);
            //Adjust batch volume.
            final BrewsTools bt = new BrewsTools();
            bt.updateTankVolumeForKegMovement(en, volumeMoved, kegs.get(0).getKegBatch());
            //Update FGI inventory.
            updateBeerInventory(en, kegs);
            //Insert a keg movement.
            final MovementTools mt = new MovementTools();
            mt.insertKegMovement(en, volumeMoved, kegs);
        }
    } 
    /***************************************************************************
     * This function modifies tank volume, beer inventory, and adds movements
     * based on the list of kegs scanned and submitted to openBrewRF.
     * @param en - Entity Manager
     * @param kegs - kegs being scanned.
     * @param bcsid - id of barrel conditioning being modified.
     **************************************************************************/
    public void doProcessBarrelAgedKegPackagingList(final EntityManager en, final List<ScanTblKeg> kegs, final int bcsid){
        if(kegs != null){
            final double volumeMoved = calcVol(kegs);
            //Adjust batch volume.
            final BarrelConditioningTools bct = new BarrelConditioningTools();
            bct.updateTankVolumeForMovement(en, volumeMoved, bcsid);
            //Update FGI inventory.
            updateBeerInventory(en, kegs);
            //Insert a keg movement.
            final MovementTools mt = new MovementTools();
            mt.insertBarrelKegMovement(en, volumeMoved, bcsid, 1);
        }
    } 
    
    private double calcVol(final List<ScanTblKeg> kegs){
        double retVal = 0;
        for(ScanTblKeg keg : kegs){
            PackageType pkg = keg.getKegPackageID();
            retVal += (double)pkg.getPackageTypeVol();
        }
        return retVal;
    }
    
    private void updateBeerInventory(final EntityManager en, final List<ScanTblKeg> kegs){
        List<PackageType> packageTypes = en.createNamedQuery("PackageType.findAll", PackageType.class).getResultList();
        List<FinishedGoodsInventory> fgis = en.createNamedQuery("FinishedGoodsInventory.findAll", FinishedGoodsInventory.class).getResultList();
        InventoryModifiers im = new InventoryModifiers();
        final int beerID = kegs.get(0).getKegBeer().getbId();
        for(final PackageType pkg : packageTypes){
            int qty = 0;
            for(final ScanTblKeg keg : kegs){
                if(keg.getKegPackageID().getPackageTypeID() == pkg.getPackageTypeID()){
                    qty++;
                }
            }
            if(qty!=0){
                for(final FinishedGoodsInventory fgi : fgis){
                    if((fgi.getFGIPackageID().getPackageTypeID() == pkg.getPackageTypeID()) 
                            && (fgi.getFGIBeerID().getbId()== beerID)){
                        //found the fgi, now update it.
                        im.doFGIAddition(en, fgi, qty);
                    }
                }
            }
        }
    }
}
